<template lang="">
  <el-container class='main-container'>
    <el-header>
      <div class="main-header">
        <p>商城后台管理系统</p>
      </div>
      <el-button type="info" @click='logOut'>退出</el-button></el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'" >
        <el-menu ref='leftMenu' background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" unique-opened :collapse='isCollapse' :default-active="activeNav" :collapse-transition='false' router>
          <div class="collapseToggle">
            <p @click='toggleCollapse'>|||</p>
          </div>
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="subItemIcon[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"  @click="saveNavState('/'+subItem.path)">
              <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  created () {
    this.getMenuList()
    this.activeNav = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menuList: [],
      subItemIcon: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activeNav: ''
    }
  },
  methods: {
    logOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取左侧菜单失败')
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activeNav = activePath
    }
  }
}
</script>
<style lang="less" scoped>
  .el-header{
    background: rgb(44, 44, 44);
    display: flex;
    justify-content: space-between;
    p{
      font-size: 20px;
      color: #fff;
    }
    button{
      margin: 10px 0;
    }
  }
  .el-aside{
    background: #545c64;
    .el-menu{
      border-right:none;
      .iconfont{
        margin-right: 10px;
      }
    }
    .collapseToggle{
      background: #4a545c;
      color: #fff;
      p{
        margin: 0;
        text-align: center;
        letter-spacing: 2px;
        padding: 10px 0;
        cursor: pointer;
      }
    }
  }
  .el-main{
    background: #eee;
  }
  .main-container{
    height: 100%;
  }
</style>
